package com.showgood.view.mediator {
	import com.greensock.TweenLite;
	import com.showgood.config.Config;
	import com.showgood.config.PopupConfig;
	import com.showgood.core.AbstractMediator;
	import com.showgood.core.GameEvent;
	import com.showgood.core.NotificationDispatcher;
	import com.showgood.core.NotificationEvent;
	import com.showgood.config.SangoConst;
	import com.showgood.model.GameViewProxy;
	import com.showgood.model.ManProxy;
	import com.showgood.model.PopupStackProxy;
	import com.showgood.model.PreloadProxy;
	import com.showgood.util.AssetsUtil;
	import com.showgood.util.MapElem;
	import com.showgood.view.component.IdNameItem;
	import com.showgood.view.FirmSlidder;
	import com.showgood.view.component.MarchPath;
	import com.showgood.vo.ArmyVO;
	import com.showgood.vo.CityVO;
	import com.showgood.vo.ManVO;
	import com.showgood.vo.RemoteMessage;
	import flash.events.Event;
	import mx.containers.TitleWindow;
	import mx.core.IFlexDisplayObject;
	import mx.events.CloseEvent;
	import org.puremvc.as3.interfaces.INotification;
	import com.showgood.view.ChuZhengDialog;
	import com.showgood.view.CityDialog;
	
	/**
	 * 出征
	 * @author bianpeng
	 */
	public class ChuZhengDialogMediator extends AbstractMediator {
		
		public function ChuZhengDialogMediator(viewComponent:Object = null) {
			super(viewComponent);
		}
		private function get chuZhengDialog():ChuZhengDialog {
			return viewComponent as ChuZhengDialog;
		}
		override public function onRegister():void {
			chuZhengDialog.addEventListener(SangoConst.CANCEL_WINDOW, onCancel);
		}
		private function onCancel(event:GameEvent):void {
			_logger.info(event);
		}
		override protected function setupNotifactionHandler(dispatcher:NotificationDispatcher):void {
			dispatcher.addHandler(SangoConst.USER_QUERY_MAN_DETAIL, onQueryManDetail);
			dispatcher.addHandler(SangoConst.SHOW_CHUZHENG_MAN_DETAIL, onShowManDetail);
			dispatcher.addHandler(SangoConst.FREE_MAN_LIST_ITEM, onUnSelectManList);
			dispatcher.addHandler(SangoConst.SELECT_CHUZHENG_TARGET, onSelectChuzhengTarget);
		}
		/**
		 * 使得list没有选中状态的item
		 * @param	notify
		 */
		private function onUnSelectManList(notify:NotificationEvent):void {
			chuZhengDialog.manList.unSelectList();
		}
		/**
		 * 查看武将详情成功, 显示一下
		 * @param	notify
		 */
		private function onShowManDetail(notify:NotificationEvent):void {
			var _manVO:ManVO = notify.data as ManVO;
			// update maninfo pane
			chuZhengDialog.manInfo.manVO = _manVO;
			// update cache
			AssetsUtil.instance.manInfoCache.put(_manVO.id, _manVO);
		}
		/**
		 * 查看武将详情
		 * @param	notify
		 */
		private function onQueryManDetail(notify:NotificationEvent):void {
			var _manId:int = notify.data as int;
			var _manVO:ManVO = AssetsUtil.instance.manInfoCache.itemFor(_manId);
			if (!_manVO) {
				var _proxy:ManProxy = sangoFacade.retrieveProxyByType(ManProxy) as ManProxy;
				_proxy.queryManDetail(RemoteMessage.REMOTE_QUERY_CHUZHENG_MAN_DETAIL, _manId);
			}
			else {
				chuZhengDialog.manInfo.manVO = _manVO;
			}
		}
		private function onSelectChuzhengTarget(notify:NotificationEvent):void {
			_logger.info(notify.data);
			// hide the chuzheng dialog first
			chuZhengDialog.visible = false;
			// show the mode dialog
			//sangoFacade.sendNotification(SangoConst.SHOW_MODE_PANE, true);
			var _p:PopupStackProxy = sangoFacade.retrieveProxyByType(PopupStackProxy) as PopupStackProxy;
			var _pane:FirmSlidder = _p.pushWindow(PopupConfig.FIRM_SLIDDER).topwindow.window as FirmSlidder;
			_pane.y = 90;
			_pane.x = Config.SCREEN_WIDTH - 1;
			// 先创建一个军队
			var gameViewProxy:GameViewProxy = sangoFacade.retrieveProxyByType(GameViewProxy) as GameViewProxy;
			gameViewProxy.prepareArmy();
			
			TweenLite.to(_pane, 0.6, { x:Config.SCREEN_WIDTH - _pane.width - 1 } );
			sangoFacade.sendNotification(SangoConst.SHOW_HINT, "请选择目标");
		}
	}

}

